	 									
package com.module.device.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;
import com.module.device.vo.DeviceBreakdown;

public class DeviceBreakdownDao  extends GenericHibernateDao<DeviceBreakdown, Long> {	
	private DeviceBreakdownDao(){}
	
	private static DeviceBreakdownDao dao;
	
	public synchronized static DeviceBreakdownDao getInstance(){
		if(dao==null)
			dao = new DeviceBreakdownDao();
		return dao;
	}
	/**
	 * 查询列表
	 * @param session
	 * @param bean
	 * @param start
	 * @param limit
	 * @return
	 * @throws SQLException
	 */
	public Page getList(Session session,DeviceBreakdown bean,Integer start, Integer limit)throws SQLException{
		Page page = new Page();
		StringBuffer sb = new StringBuffer();
	    StringBuffer condition = new StringBuffer();
		sb.append("from DeviceBreakdown as obj where 1=1");
		String deviceTypeName="";
		if(bean!=null)
		{
			deviceTypeName=bean.getSearchDeviceTypeName();
		}
		if(!"".equals(deviceTypeName)&&!(deviceTypeName==null))
		{
			condition.append(" and obj.deviceType='"+bean.getSearchDeviceTypeName()+"'");
			sb.append(condition);
		}
		System.out.println("===================SQL:"+sb);
		Query query = session.createQuery(sb.toString());  
		query.setFirstResult(start).setMaxResults(limit);		
		Query totalQuery = session
		.createQuery("select count(*) from DeviceBreakdown as obj where 1=1"+condition);
		page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
		page.setRows(query.list());	
		return page;
	}
	
	/**
	 * 通过设备类型名称查询列表
	 * @param session
	 * @param DeviceTypeName
	 * @param start
	 * @param limit
	 * @return
	 * @throws SQLException
	 */
	public Page findByDeviceTypeName(Session session,String DeviceTypeName,Integer start, Integer limit)throws SQLException{
		Page page = new Page();
		StringBuffer sb = new StringBuffer();
	    StringBuffer condition = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		sb.append("from DeviceBreakdown as obj where 1=1 and obj.deviceEntity.deviceType.deviceTypeName='"+DeviceTypeName+"'");
		sb.append(condition);
		Query query = session.createQuery(sb.toString());  
		query.setFirstResult(start).setMaxResults(limit);		
		Query totalQuery = session
		.createQuery("select count(*) from DeviceBreakdown as obj where 1=1 and obj.deviceEntity.deviceType.deviceTypeName='"+DeviceTypeName+"'");		
		page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
		page.setRows(query.list());	
		return page;
	}
	
	
	
}

